<?php

/*
 * loads image, resize, output
 *
 */

class Image {

	var $imagePath;
	var $contentType;
	var $caption;
	var $maxHeight;
	var $maxWidth;
	

	/**
	 * constructor
	 *
	 * @return 	void
	 */
	function __construct($gallery, $image, $maxHeight, $maxWidth) {
		ob_start();
		$this->maxHeight = $maxHeight;
		$this->maxWidth = $maxWidth;

		global $c_galleryFolder;

		## build image path
		$this->imagePath = ROOT . '/' . $c_galleryFolder . '/' . $gallery . '/' . $image;

		## load config
		$configFile = ROOT . '/' . $c_galleryFolder . '/' . $gallery . '/_gallery_config.php';
		if (file_exists($configFile)) {
			require_once($configFile);
		}
		## load caption
		$this->caption = $image2captionArray[$image];

		if ($maxHeight > 0 || $maxWidth > 0) {
			$this->imageResource = $this->resizeImage($this->imagePath, $maxHeight, $maxWidth);
		} else {
			$this->imageResource = $this->getImageResource($imagePath);
		}
		## check for errors
		$outputs = ob_get_contents();
		ob_end_clean();
   }

	/**
	 * resizes an image to new height/width
	 *
	 * @param 	string		name of uploaded field
	 * @param 	int			max height of resized image
	 * @param 	int			max width of resized image
	 * @return 	resource	resized image
	 */
	function resizeImage($originalImage,$maxHeight,$maxWidth){

	    ## Get the original geometry and calculate scales
		$imageInfoArray = getimagesize($originalImage);
		if (!$imageInfoArray) {
			trigger_error('Image ' . $originalImage . ' could not be loaded.', E_USER_ERROR);
			return false;
		}
		$originalWidth = $imageInfoArray[0];
		$originalHeight = $imageInfoArray[1];

		if ($originalWidth > $originalHeight) {
	    	$newHeight = round(($originalHeight * $maxWidth) / $originalWidth);
			$newWidth = $maxWidth;
			if ($newHeight > $maxHeight) {
	    		$newWidth = round(($originalWidth * $maxHeight) / $originalHeight);
				$newHeight = $maxHeight;
			}
		} else {
	    	$newWidth = round(($originalWidth * $maxHeight) / $originalHeight);
			$newHeight = $maxHeight;
			if ($newWidth > $maxWidth) {
		    	$newHeight = round(($originalHeight * $maxWidth) / $originalWidth);
				$newWidth = $maxWidth;
			}
		}

		## load original image
		$imageTmp = $this->getImageResource($originalImage);

	    ## create canvas for resizing
	    $imageResized = imagecreatetruecolor($newWidth, $newHeight);

	    imagecopyresampled($imageResized, $imageTmp, 0, 0, 0, 0, $newWidth, $newHeight, $originalWidth, $originalHeight);
		return $imageResized;
	}



	/**
	 * loads a image into a resource
	 *
	 * @param 	string		path to image
	 * @return 	resource	image resource
	 */
	function getImageResource($imagePath) {
		if (!isset($imagePath)) {
			$imagePath = $this->imagePath;
		}

		$ext = $this->getFileExtension($imagePath);
		switch ($ext) {
			case 'jpg':
			case 'jpeg':
	    		$imageResource = imagecreatefromjpeg ($imagePath);
				$this->contentType = 'image/jpeg';
			break;
			case 'gif':
	    		$imageResource = imagecreatefromgif ($imagePath);
				$this->contentType = 'image/gif';
			break;
			case 'png':
	    		$imageResource = imagecreatefrompng ($imagePath);
				$this->contentType = 'image/png';
			break;
		}

		return $imageResource;
	}


	/**
	 * outputs an image
	 *
	 * @param 	resource	image
	 * @return 	void
	 */
	function outputImageResource($imageResource = NULL){
		if (!isset($imageResource)) {
			$imageResource = $this->imageResource;
		}
		if (!isset($imageResource)) {
			trigger_error('Bad imageResource.', E_USER_ERROR);
		}
	   	header("Content-Type: " . $this->contentType);
	    imagejpeg($imageResource, null, 100);
	    imageDestroy($imageResource);
	}


	/**
	 * gets the extension of a filename
	 *
	 * @param 	string	filename
	 * @return 	string	lowercase extension
	 */
	static function getFileExtension($filename) {
		$tmpArray = explode('.', $filename);
		$ext = $tmpArray[count($tmpArray) - 1];
		$ext = strtolower(trim($ext));
		return $ext;
	}


	/**
	 * checks if extension is allowed
	 *
	 * @param 	string	extension
	 * @return 	bool	result
	 */
	static function isAllowedImage($ext) {
		global $c_allowedImageExtensions;
		if (in_array(strtolower(trim($ext)), $c_allowedImageExtensions)) {
			return true;
		}
		return false;
	}
}


?>